package test;

import com.medreams.dao.UserDao;
import com.medreams.domain.User;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class test {

    public static void main(String[] args) throws IOException {
        //1.读取配置文件
        InputStream resourceAsStream = Resources.getResourceAsStream("SqlMapConfig.xml");
        //2.创建工厂 SqlSessionFactory
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        SqlSessionFactory build = builder.build(resourceAsStream);
        //3.使用工厂生产对象SqlSession
        SqlSession sqlSession = build.openSession();
        //4.使用SqlSession创建dao接口的代理对象
        UserDao mapper = sqlSession.getMapper(UserDao.class);
        //5.使用代理对象执行方法
        List<User> all = mapper.finAll();
        for (User user : all) {
            System.out.println(user);
        }

        //6.释放对象
        sqlSession.close();
        resourceAsStream.close();

    }

}
